package edu.isquen.core.admin.action;

import edu.isquen.core.admin.model.Superuser;
import edu.isquen.core.admin.service.SuperuserService;
import edu.isquen.core.base.action.BaseAction;
import edu.isquen.util.factory.ServiceFactory;

public class AdminAction extends BaseAction<Superuser> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5488436276449155738L;
	
	
	public AdminAction() throws Exception {
		super(ServiceFactory.createProcess(SuperuserService.class), new Superuser());
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	public String doLogin(){
		Superuser superuser=(Superuser) this.getValObj();
		try {
			Superuser loginUser=((SuperuserService) service).doLogin(superuser.getLoginno(), superuser.getLoginpwd());
			if(loginUser==null){
				this.addActionMessage("Exception: The user not exit");
				return INPUT;
			}
			this.putIntoSession(Superuser.ADMIN_USER, loginUser);
		} catch (Exception e) {
			this.addActionMessage("Exception: "+e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String doSave(){
		Superuser superuser=(Superuser) this.getValObj();
		if(superuser!=null){
			try {
				((SuperuserService) service).doSaveOrUpdate(superuser);
			} catch (Exception e) {
				this.addActionMessage("Exception: "+e.getMessage());
				return INPUT;
			}
		}
		return SUCCESS;
	}
}
